package handle

import (
	"AcsGo/internal/domain"
	"AcsGo/internal/model"
	"AcsGo/internal/util"
	"database/sql"
	"strconv"
)

// 借书

func HandlebookRenewal(str string) (string, error) {
	success := ""
	rdbm := ""
	bkbm := ""
	dueDate := ""
	loadDate := ""
	bkTitle := ""
	currentDate, dueDate1, currentTime, _ := util.GetFormattedDatesAndTime()
	lg.Debugf("currentDate: %v, dueDate: %v, currentTime: %v", currentDate, dueDate1, currentTime)
	var queryResponse *domain.BookRenewalResponse
	queryRequest, err := domain.HandleBookRenewalRequest(str)
	if err != nil {
		queryResponse = domain.InitBookRenewalResponse(success, rdbm, bkbm, dueDate, loadDate, bkTitle)
		sb, _ := queryResponse.StringFormat()
		lg.Debugf("HandleBookRenewalRequest: %v", sb)
		return sb, err
	}
	lg.Debugf("InitBookRenewalResponse: %v", queryRequest)
	db, err := model.ConnectDB()
	if err != nil {
		return "", err
	}
	publicRecord, err := model.GetPbBookRecordBy(db, queryRequest.ItemIdentifier.Value)
	if err != nil {
		queryResponse = domain.InitBookRenewalResponse(success, rdbm, bkbm, dueDate, loadDate, bkTitle)
		sb, _ := queryResponse.StringFormat()
		lg.Debugf("GetPbBookRecordBy: %v", sb)
		return sb, err
	}

	bookid, _ := strconv.Atoi(publicRecord.BKRECD)

	book, err := model.GetBook(db, bookid)
	if err != nil {
		queryResponse = domain.InitBookRenewalResponse(success, rdbm, bkbm, dueDate, loadDate, bkTitle)
		sb, _ := queryResponse.StringFormat()
		lg.Debugf("GetBook: %v", sb)
		return sb, err
	}

	reader, err := model.GetReaderByBm(db, queryRequest.PatronIdentifier.Value)
	if err != nil {
		queryResponse = domain.InitBookRenewalResponse(success, rdbm, bkbm, dueDate, loadDate, bkTitle)
		sb, _ := queryResponse.StringFormat()
		lg.Debugf("GetReader: %v", sb)
		return sb, err
	}
	// update t_publicrecord set yhrq ='20250122' , xjtms ='1' where barbm ='ST000001'
	publicRecord.XJTMS = sql.NullString{String: "1", Valid: true}
	publicRecord.YHRQ = currentDate
	err = model.UpdatePublicRecord(db, publicRecord)
	if err != nil {
		return "", err
	}

	// insert into t_processlog ( process , rdbm , rdmc , bkbm , indexnb , titile , admintm , admin , yhrq , mny ,
	// adminsj , rdid , bkid , ltype , unit , adress ) values ( '续借' , '0600039733' , 'CS' , 'ST000001' ,
	// 'K825.42/1' , '一问一世界' , '20240725' , 'tsg' , '20250122' , '' ,
	// '11:11:37' , '167' , '47644' , '02' , '' , '01' )

	var processLog = model.ProcessLog{
		Process: "续借",
		RDBM:    reader.BM,
		RDMC:    reader.Name,
		BKBM:    publicRecord.BARBM,
		IndexNB: book.IndxNb,
		Titile:  book.Name,
		Admintm: currentDate,
		Admin:   "tsg",
		YHRQ:    dueDate1,
		MNY:     book.Price,
		AdminSJ: currentTime,
		RDID:    sql.NullString{String: strconv.Itoa(reader.ID), Valid: true},
		BKID:    sql.NullString{String: strconv.FormatInt(book.ID, 10), Valid: true},
		LType:   sql.NullString{String: reader.LType, Valid: true},
		Unit:    sql.NullString{String: reader.Unit.String, Valid: true},
		Adress:  sql.NullString{String: reader.Address.String, Valid: true},
	}
	err = model.InsertProcessLog(&processLog, db)
	if err != nil {
		return "", err
	}

	success = "1"
	rdbm = reader.BM
	bkbm = publicRecord.BARBM
	dueDate = dueDate1
	if publicRecord.JCRQ.Valid {
		loadDate = publicRecord.JCRQ.String
	} else {
		loadDate = ""
	}
	bkTitle = book.Name

	queryResponse = domain.InitBookRenewalResponse(success, rdbm, bkbm, dueDate, loadDate, bkTitle)
	sb, _ := queryResponse.StringFormat()
	lg.Debugf("success: %v", sb)
	return sb, err
}
